package com.songshuai.sales.tool.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.songshuai.sales.tool.dto.request.soptype.*;
import com.songshuai.sales.tool.dto.response.label.QueryLabelsPageResDto;
import com.songshuai.sales.tool.dto.response.soptype.SopTypeDto;
import com.songshuai.sales.tool.service.SopTypeService;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.common.annotation.RequestJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Api("SopType")
@RestController
@RequestMapping("/api/sopType/")
public class SopTypeController {

    @Autowired
    private SopTypeService sopTypeService;

    @ApiOperation(value = "查询分类列表")
    @GetMapping("querySopTypeList")
    public QueryRes querySopTypeList(BaseParmDto<String> request){
        try {
            if(request.getPageIndex() > 0){
                Page<List<SopTypeDto>> page = PageHelper.startPage(request.getPageIndex(), request.getPageSize());
                List<SopTypeDto> sopTypeList = sopTypeService.querySopTypeList(request);
                return QueryPageRes.setQueryResData(sopTypeList, page.getTotal());
            }else{
                List<SopTypeDto> sopTypeList = sopTypeService.querySopTypeList(request);
                return QueryRes.setResData(sopTypeList);
            }
        } catch (Exception e) {
            log.error("查询分类列表异常：{}",e);
            return new QueryRes(ResultStatus.FAILOfficial);
        }
    }

    @ApiOperation(value = "上升下降分类排序")
    @GetMapping("sopTypeSort")
    public QueryRes sopTypeSort(@RequestParam("typeId") String typeId, @RequestParam("orderBy") String orderBy){
        try {
            Integer flag = sopTypeService.sopTypeSort(typeId, orderBy);
            if(flag == 1){
                return new QueryRes(ResultStatus.FAILOfficial.getErrorCode(), "分类不存在");
            }else if(flag == 2){
                String msg = "";
                if("desc".equals(orderBy)){
                    msg = "该分类排序已达到最后位置，不能再下降";
                }else{
                    msg = "该分类排序已达到最前位置，不能再上升";
                }
                return new QueryRes(ResultStatus.FAILOfficial.getErrorCode(), msg);
            }
        } catch (Exception e) {
            log.error("分类排序异常：{}", e);
            return new QueryRes(ResultStatus.FAILOfficial.getErrorCode(), "分类排序失败");
        }
        return new QueryRes();
    }

    @ApiOperation(value = "添加或编辑sop分类")
    @PostMapping("saveOrUpdateSopType")
    public QueryRes saveOrUpdateSopType(@RequestBody SopTypeReqDto sopTypeReqDto){
        String msg = "";
        Integer code = ResultStatus.SUCCESS.getErrorCode();
        String id = sopTypeReqDto.getId();
        try {
            Integer flag = sopTypeService.saveOrUpdateSopType(sopTypeReqDto);
            if(flag == 1){
                if(StringUtils.hasText(id)){
                    msg = "分类名称已存在";
                    code = ResultStatus.ERROR_UPDATE_FAILED.getErrorCode();
                }else{
                    msg = "分类名称不能为空";
                    code = ResultStatus.ERROR_CREATE_FAILED.getErrorCode();
                }
            }else if(flag == 2){
                if(StringUtils.hasText(id)){
                    msg = "分类不存在";
                    code = ResultStatus.ERROR_UPDATE_FAILED.getErrorCode();
                }else{
                    msg = "分类名称已存在";
                    code = ResultStatus.ERROR_CREATE_FAILED.getErrorCode();
                }
            }
        } catch (Exception e) {
            if(StringUtils.hasText(id)){
                log.error("编辑sop分类异常：{}", e);
                return new QueryRes(ResultStatus.ERROR_UPDATE_FAILED.getErrorCode(), ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg());
            }else{
                log.error("新增sop分类异常：{}", e);
                return new QueryRes(ResultStatus.ERROR_CREATE_FAILED.getErrorCode(), ResultStatus.ERROR_CREATE_FAILED.getErrorMsg());
            }
        }
        return new QueryRes(code, msg);
    }

    @ApiOperation(value = "删除分类")
    @PostMapping("deleteSopType")
    public QueryRes deleteSopType(@RequestJson("typeId") String typeId){
        try {
            if(StringUtils.isEmpty(typeId)){
                return new QueryRes(ResultStatus.ERROR_DELETE_FAILED.getErrorCode(), "参数必传");
            }
            Integer flag = sopTypeService.deleteSopTypeById(typeId);
            if(flag == 3){
                return new QueryRes(ResultStatus.ERROR_DELETE_FAILED.getErrorCode(), "分类不存在");
            }else if(flag == 2){
                return new QueryRes(ResultStatus.ERROR_DELETE_FAILED.getErrorCode(), "请先删除关联内容");
            }else if(flag == 1){
                return new QueryRes(ResultStatus.ERROR_DELETE_FAILED.getErrorCode(), "请先删除下级分类");
            }
        } catch (Exception e) {
            log.error("删除分类异常：{}", e);
            return new QueryRes(ResultStatus.ERROR_DELETE_FAILED);
        }
        return new QueryRes();
    }

    @ApiOperation("获取指定sop分类信息")
    @RequestMapping("getSopTypeDetail")
    public QueryRes getSopTypeDetail(@RequestParam("typeId") String typeId){
        try {
            if(StringUtils.isEmpty(typeId)){
                return new QueryRes(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(), "参数必传");
            }
            SopTypeDto sopTypeDto = sopTypeService.getSopTypeDetailByTypeId(typeId);
            if(null == sopTypeDto){
                return new QueryRes(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(), "指定sop分类不存在");
            }
            return QueryRes.setResData(sopTypeDto);
        } catch (Exception e) {
            log.error("获取指定sop分类信息异常：{}", e);
            return new QueryRes(ResultStatus.ERROR_SEARCH_FAILED);
        }
    }

}
